package com.example.test_load_pic_rough;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.test_load_pic_rough.adapter.ChromometerAdapter;
import com.example.test_load_pic_rough.bean.ItemInfo;
import com.xuexiang.xui.adapter.recyclerview.DividerItemDecoration;

import java.util.ArrayList;

public class ChronometerActivity extends AppCompatActivity {

    private ChromometerAdapter chromometerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chronometer);

        initRv();
        initBtn();
    }

    private void initBtn() {
        Button button = findViewById(R.id.addItem);
        button.setOnClickListener(v -> {
            int itemCount = chromometerAdapter.getItemCount();
            ItemInfo itemInfo = new ItemInfo();
            itemInfo.setCode((itemCount+1)+"");
            itemInfo.setCreateTime(System.currentTimeMillis());
            chromometerAdapter.addItem(itemInfo);
        });


        Button button1 = findViewById(R.id.refresh);
        button1.setOnClickListener(v -> {
            chromometerAdapter.notifyDataSetChanged();
        });
    }

    private void initRv() {
        RecyclerView rv  = findViewById(R.id.rv);
        ArrayList<ItemInfo> list = new ArrayList<>();
        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setCode("1");
        itemInfo.setCreateTime(System.currentTimeMillis());
        list.add(itemInfo);
        chromometerAdapter = new ChromometerAdapter(list);
        rv.setAdapter(chromometerAdapter);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
    }
}